from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph
from .api_instance import api_instance

class ActionAskAboutSpecificRelation(Action):
    def name(self) -> Text:
        return "action_ask_about_specific_relation"

    async def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        person_name = next(tracker.get_latest_entity_values("person"), None)
        relation_name = next(tracker.get_latest_entity_values("relation"), None)

        print(f"person_name:{person_name}, relation_name:{relation_name}")

        if person_name is None:
            dispatcher.utter_message("不好意思，我不清楚你问的是什么人。", attachment="finish")
            return []
        
        if relation_name is None:
            dispatcher.utter_message("不好意思，我不太清楚你问的是什么关系。", attachment="finish")
            return []

        relation_types = {
            "父亲": ["父亲", "爹", "亲爹", "老子", "令尊", "尊父", "爸爸","爸" ],
            "母亲": ["母亲", "娘", "令堂", "亲娘", "妈妈", "妈"] ,
            "哥哥": ["哥", "哥哥", "兄长", "堂兄", "表兄", "表哥"],
            "弟弟": ["弟", "弟弟", "令弟", "堂弟", "表弟"] ,
            "姐姐": ["姐", "姐姐", "姊姊", "姊", "令姊"],
            "妹妹": ["妹", "妹妹", "令妹"],
            "夫人": ["爱人", "妻子", "老婆", "媳妇", "媳妇儿", "夫人", "尊夫人"],
            "夫君": ["丈夫", "爱人", "老公", "夫君", "尊夫君"],
            "祖父": ["祖父", "爷爷", "令祖父", "爷", "父亲的父亲"],
            "祖母":["祖母", "奶奶", "令祖母", "奶", "父亲的母亲"],
            "外祖父": ["外祖父", "姥爷", "令外祖父"],
            "外祖母": ["外祖母", "姥姥", "令外祖母", "姥"],
            "儿子": ["儿", "孩子", "儿子", "令郎"],
            "女儿": ["女儿", "孩子", "令爱", "令嫒"],
            "朋友": ["朋友", "友人", "好友", "密友", "挚友"],
            "敌人": ["敌人", "对手", "大敌", "敌对方"],
            "大臣": ["大臣", "臣子", "手下的官员"],
            "手下": ["手下", "部下", "下属"]
        }

        ask_relation_type = ""

        for relation_type, relation_alias in relation_types.items():
            if relation_name in relation_alias:
                ask_relation_type = relation_type
                break

        if ask_relation_type == "":
            api_instance.ask(tracker.events, dispatcher)
            #dispatcher.utter_message("不好意思，我不太清楚你询问的是什么关系。", attachment="finish")
            return []
        
        #print(ask_relation_type)

        person_list = graph.query_specific_relation(person_name, ask_relation_type)

        if not person_list:
            dispatcher.utter_message("不好意思，我不太清楚你问的是谁。", attachment="finish")
            return []
        
        message = ""

        #print(person_list)

        if len(person_list) == 1:
            person_node = person_list[0]["p2"]
            relation_person_name = person_node["name"]
        
            message = f"{person_name}的{relation_name}是{relation_person_name}。\n"
        else:
            person_name_list = []
            for item in person_list:
                if item["p2"]["name"] not in person_name_list:
                    person_name_list.append(item["p2"]["name"])

            if len(person_name_list) == 1:
                relation_person_name = person_name_list[0]
                person_node = person_list[0]["p2"]
                message = f"{person_name}的{relation_name}是{relation_person_name}。\n"
            else:
                message = (f"{person_name}的{relation_name}有" + ("，".join(person_name_list)) + "。\n")
        
        api_instance.optimize_response(tracker.events, message, dispatcher)

        return []